<?php
/**
 * Created by PhpStorm.
 * User: dell
 * Date: 2018/7/16
 * Time: 16:03
 */
namespace app\repository;
use app\repository\wechat\Template;
use app\repository\wechat\Wechat;

class PushMess extends Repository
{
    /**
     * 发送模板消息
     * @param array $aids 用户id
     * @param $data 模板数据
     * @param $templateId 模板id
     * @param $url
     * @return int
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function sendTemplate(array $aids,$data,$templateId,$url){
        $rows = db('admin')->where('admin_id','in',$aids)->select();

        $tem = new Template();

        foreach ($rows as $v){
            $tdata = [
                'touser'=>$v['openid'],
                'template_id'=>$templateId,
                'url'=>$url,
                'data'=>$data
            ];

            $res = $tem->send(json_encode($tdata));
            $log = [
                'openid'=>$v['openid'],
                'template_id'=>$templateId,
                'url'=>$url,
                'admin_id'=>$v['admin_id'],
                'data'=>json_encode($tdata),
                'result'=>json_encode($res)
            ];
            db('wx_log')->insertGetId($log);
        }
        return 1;
    }

}